package com.hqd.jjvm.classformat.constantpool;

import com.hqd.jjvm.classformat.ClassFile;
import com.hqd.jjvm.util.HexStrTransformUtil;
import lombok.Getter;

/**
 * 常量池基础类
 *
 * @author hqd
 */
@Getter
public class ConstantPool {
    protected ConstantInfoType tag;
    protected ClassFile classFile;

    public ConstantPool(String tagStr) {
        this.tag = ConstantInfoType.getType(HexStrTransformUtil.parseHexToInt(tagStr));
    }

    public ConstantPool(ConstantInfoType tag, ClassFile classFile) {
        this.tag = tag;
        this.classFile = classFile;
    }

    public Object getVal() {
        return null;
    }

    /**
     * 检查下标是否越界
     *
     * @param index
     */
    protected void checkPoolIndex(int index) {
        if (index < 0 || index > classFile.getConstantPoolCount()) {
            throw new ClassFormatError(String.format(" Invalid constant pool index %s in class file %s ", index, classFile.getClassFileInfo().getFileName()));
        }
    }

}
